<?php
	include("../Action/config.php");
	
	$json = urldecode(file_get_contents("php://input"));
	$arrayJson = json_decode($json,true);
	
	$phone_num = $arrayJson['phone_num'];
	$child_type = $arrayJson['child_type'];
	$child_name = $arrayJson['child_name'];		
	
	$sql = "SELECT * FROM PARENT WHERE phone_num = '$phone_num'";
	$result = mysql_query($sql) or die("fail query");
	$count = mysql_num_rows($result) or die("fail : we can't find phone number");
	
	$row = mysql_fetch_array($result) or die("fail fetch");
	
	if($count == 1){ // success to insert data into DB
		$parent_num = $row['parent_num'];
		$sql = "INSERT INTO CHILD (parent_num, child_type, child_name, free_time) 
			VALUES ('$parent_num', '$child_type', '$child_name', 1)";
		mysql_query($sql);
		
		$sql = "SELECT * FROM CHILD WHERE parent_num = '$parent_num' and child_name = '$child_name'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		$child_num = $row['child_num'];
		
		echo $child_num;
	}else{ // fail to insert data into DB
		echo "fail";
	}
?>